// Adobe Function
// This function returns the appropriate reference,
// depending on the browser.
function getFlexApp(appName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[appName];
	} else {
		return document[appName];
	}
}

function getDataPointType(dataPointName) {
	if (dataPointName == "Square") {
		return "0";
	} else if (dataPointName == "Square Hollow") {
		return "1";
	} else if (dataPointName == "Circle") {
		return "2";
	} else if (dataPointName == "Circle Hollow") {
		return "3";
	} else if (dataPointName == "Diamond") {
		return "4";
	} else if (dataPointName == "Diamond Hollow") {
		return "5";
	} else if (dataPointName == "Cross") {
		return "6";
	} else if (dataPointName == "Plus") {
		return "7";
	} else {
		return "8";
	}
}

function storeCookieSettings(application, id, cookieSettings) {
	var expires = new Date();
	expires = new Date(expires.getTime() + 1000 * 60 * 60 * 24 * 365);

	for ( var i = 0; i < cookieSettings.length; i += 2) {
		document.cookie = application + '/' + id + '/' + cookieSettings[i]
				+ '=' + cookieSettings[i + 1] + '; expires='
				+ expires.toGMTString() + ';';
	}
}

function storeSingleCookieSetting(application, id, name, value) {
	var expires = new Date();
	expires = new Date(expires.getTime() + 1000 * 60 * 60 * 24 * 365);
	document.cookie = application + '/' + id + '/' + name + '=' + value
			+ '; expires=' + expires.toGMTString() + ';';
}

function deleteSingleCookieSetting(application, id, name) {
	// Set the date to today one year ago
	var expires = new Date();
	expires = new Date(expires.getTime() - 1000 * 60 * 60 * 24 * 365);
	document.cookie = application + '/' + id + '/' + name + '=' + 'Expired'
			+ '; expires=' + expires.toGMTString() + ';';
}

function deleteCookieForApplication(application, id) {
	if (document.cookie) {
		var i = 0;
		var cookie = document.cookie;

		// Set the date to today one year ago
		var expires = new Date();
		expires = new Date(expires.getTime() - 1000 * 60 * 60 * 24 * 365);

		while (cookie != '') {
			var start = (i == 0) ? 0 : 1;
			cname = cookie.substring(start, cookie.search('='));

			var matchesApplication = cname.substring(0, application.length + id.length + 2) == application + '/' + id + '/';
			if (matchesApplication) {
				document.cookie = cname + '=' + 'Expired' + '; expires=' + expires.toGMTString() + ';';
			}

			i += 2;

			var index = cookie.search(';') + 1;
			if (index == 0) {
				index = cookie.length
			}

			cookie = cookie.substring(index, cookie.length);
		}
		getFlexApp(application).applicationCookiesDeleted();
	}
}

function getCookieSettings(application, id) {
	if (document.cookie) {
		var settings = new Array();
		var i = 0;
		var cookie = document.cookie;
		var applicationCookieFound = false;
		var firstRun = true;

		while (cookie != '') {
			var start = (firstRun) ? 0 : 1;
			cname = cookie.substring(start, cookie.search('='));
			cvalue = cookie.substring(cookie.search('=') + 1, cookie
					.search(';'));

			if (cvalue == ' ' + cname + '=') {
				cvalue = cookie
						.substring(cookie.search('=') + 1, cookie.length);
			}

			var matchesApplication = cname.substring(0, application.length + id.length + 2) == application + '/' + id + '/';
			if (matchesApplication && cvalue != 'Expired') {
				applicationCookieFound = true;
				cname = cname.substring(application.length + id.length + 2, cname.length);
				settings[i] = cname;
				settings[i + 1] = cvalue;
				i += 2;
			}

			var index = cookie.search(';') + 1;
			if (index == 0) {
				index = cookie.length
			}

			cookie = cookie.substring(index, cookie.length);
			firstRun = false;
		}

		vis.settings = settings;

		if (applicationCookieFound) {
			getFlexApp(application).recieveCookieSettings(vis.settings);
		} else {
			getFlexApp(application).noCookieFound();
		}
	} else {
		getFlexApp(application).noCookieFound();
	}
}
